<?php

namespace App\Livewire\Admin\Role;

use Livewire\Attributes\Layout;
use Livewire\Component;
use Spatie\Permission\Models\Role;

#[Layout('components.admin.layout')]
class Update extends Component
{
    public $name;
    public $description;
    public Role $role;

    public function mount(Role $role) {
        $this->role = $role;
        $this->fill($role->only('name', 'description'));
    }

    public function store()
    {
        $this->role->name = $this->name;
        $this->role->description = $this->description;
        $res = $this->role->save();
        if ($res) {
            $html = view('admin.role.list-item', ['role' => Role::find($this->role['id'])])->render();
            $this->dispatch('update-row', [
                'html' => $html,
                'id'  => $this->role['id'],
            ]);
            $this->dispatch('close-modal');
        }
    }

    public function render()
    {
        return view('livewire.admin.role.update');
    }
}
